<?php
namespace app\admin\controller;
require '../vendor/autoload.php';

use think\Db;
use think\Controller;
class Goods extends Controller{
    public function index(){
        $res_goods = Db::table('goods')->order('id desc')->paginate(10);
        //追加所属分类和供货商
        foreach ($res_goods as $k=>$v){
            $category_name = Db::table('goods_category')->where(array('id'=>$v['goods_category_id']))->value('name');
            $res_goods[$k]['category_name'] = $category_name;
            $supply_name = Db::table('supply')->where(array('id'=>$v['supply_id']))->value('name');
            $res_goods[$k]['supply_name'] = $supply_name;
        }
//        var_dump($res_goods);exit;
        $this->assign('res_goods',$res_goods);
        return view('index');
    }
    //添加商品
    public function add(){
        if($_POST){
           //保持商品信息
            $goods_category_id = $_POST['cid'];
            $goods_name= $_POST['goods_name'];
            $contents= $_POST['contents'];
            $supply_id= $_POST['supply_id'];
            $status = $_POST['status'];
            $data=array(
                'goods_name'=>$goods_name,
                'goods_category_id'=>$goods_category_id,
                'supply_id'=>$supply_id,
                'create_time'=>time(),
                'contents'=>$contents,
                'block'=>$_POST['block'],
                'price'=>$_POST['price'],
                'sales_price'=>$_POST['sales_price'],
                'sold'=>$_POST['sold'],
                'show_sold'=>$_POST['show_sold'],
                'stock'=>$_POST['stock'],
                'show_stock'=>$_POST['show_stock'],
                'status'=>$status,
            );
            $res = Db::table('goods')->insertGetId($data);
//            var_dump($res);exti;
            //保存相册信息
            //保存关联商品
            if($res){
                //判断是否是单规格商品
                if($_POST['block']){//如果不为空代表是多规格商品
                    //获取规格值数组
                    $GooodsSpecificatonValue = $_POST['GooodsSpecificatonValue'];
                    $re_dikaer = $this->dikaer($GooodsSpecificatonValue);//使用笛卡儿积
                    foreach ($re_dikaer as $k=>$v){
                        $goods_attribute_ids = '';//连接规格值的字符串
                        foreach ($v as $kk=>$vv){
                            $goods_attribute_ids = $goods_attribute_ids.$vv.'#';
                        }
                        $goods_attribute_ids = substr($goods_attribute_ids, 0, -1);
                        //添加到goods_relation表中
                        $relation_data = array(
                            'goods_id'=>$res,
                            'goods_attribute_ids'=>$goods_attribute_ids,
                        );
                        $res_relation = Db::table('goods_relation')->insert($relation_data);
                    }
                    echo json_encode(array('error_code' => 1, 'error_string' => '', 'goods' => $res));exit;
                }
            }
            echo json_encode(array('error_code' => 2, 'error_string' => ''));exit;//单规格商品，直接跳转到商品列表页
        }else{
            $GoodsCategory = Db::table('goods_category')->where(array('status'=>1))->select();
            $this->assign('nodes',json_encode($GoodsCategory));
            return view("edit");
        }
    }
    //获取下拉的规格名
    public function getname(){
        //获取分类id
        $id = $_POST['id'];
        if(empty($id)){
            echo json_encode(array('error_code' => 0, 'error_string' => '', 'datas' => ''));exit;
        }else{
            $re = Db::table('goods_category')->where(array("id"=>$id))->find();
            //判断是否是顶级分类
            $num = Db::table('goods_category')->where(array('parent_id'=>$re['id']))->select();
            if(!empty($num)){
                echo json_encode(array('error_code' => 0, 'error_string' => '', 'datas' =>''));exit;
            }else{
                //查询规格名
                $re_name = Db::table('goods_specification')->where(array('cid'=>$re['id']))->select();
                //查询规格名下对应的所以规格值
                foreach ($re_name as $k=>$vals){
                    $re_values = Db::table('goods_specification_value')->where(array('sid'=>$vals['id']))->select();
                    $re_name[$k]['GoodsSpecificationValue'] = $re_values;
                }
                if(empty($re_name)){
                    echo json_encode(array('error_code' => -1, 'error_string' => '', 'datas' =>''));exit;
                }else{
//                    var_dump($re_name);exit;
                    echo json_encode(array('error_code' => 1, 'error_string' => '', 'datas' => $re_name));exit;
                }
            }
        }
    }
    /*
     * 追加多规格商品
     */
    function block(){
        $goods_id = input('goods_id');
        $this->assign('goods_id',$goods_id);
        $re_goods = Db::table('goods_relation')->where(array('goods_id'=>$goods_id))->select();
//        var_dump($re_goods);exit;
        //查询规格名
        $goods_attribute_ids = $re_goods[0]['goods_attribute_ids'];
        $goods_attribute_ids = explode('#',$goods_attribute_ids);
        $specification_name = array();
        foreach ($goods_attribute_ids as $k=>$v){
            $sid = Db::table('goods_specification_value')->where(array('id'=>$v))->value("sid");
            $name = Db::table('goods_specification')->where(array('id'=>$sid))->value("name");
            $specification_name[] =$name;
        }
        $this->assign('specification_name',$specification_name);
        //查询规格值
        foreach ($re_goods as $k=>$v){
            $arr = array();
            $attribute_ids = explode('#',$v['goods_attribute_ids']);
            foreach ($attribute_ids as $kk=>$vv){
                $sid = Db::table('goods_specification_value')->where(array('id'=>$vv))->value("value");
                $arr[$vv] = $sid;
            }
            $re_goods[$k]['guige'] = $arr;
            //追加商品名称
            $re_goods[$k]['goods_name'] = Db::table('goods')->where(array('id'=>$v['goods_id']))->value("goods_name");
        }
//        var_dump($re_goods);exit;
        $this->assign('goods',$re_goods);
        return view('block');
    }
    /*
     * 添加商品规格
     */
    function add_blok(){
        $res = $_POST;
//        var_dump($res);exit;
        $datas = array();
        foreach ($res['price'] as $kk=>$vv){
            Db::table('goods_relation')->where('id',$res['id'][$kk])
                ->update([
                            'price'=>$vv,
                            'sales_price'=>$res['sales_price'][$kk],
                            'stock'=>$res['stock'][$kk],
                            'sold'=>$res['sold'][$kk],
                            'show_sold'=>$res['show_sold'][$kk],
                            'show_stock'=>$res['show_stock'][$kk],
                        ]);
        }
        $this->redirect("index");
    }
    /*
     * 笛卡儿积，商品规格值
     */
    function dikaer($arr){
        $arr1 = array();
        $result = array_shift($arr);
        while($arr2 = array_shift($arr)){
            $arr1 = $result;
            $result = array();
            foreach($arr1 as $v){
                foreach($arr2 as $v2){
                    if(!is_array($v))$v = array($v);
                    if(!is_array($v2))$v2 = array($v2);
                    $result[] = array_merge_recursive($v,$v2);
                }
            }
        }
        return $result;
    }
}